/*
 * SchedulerDocument.java
 *
 * Created on November 7, 2006, 9:29 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler;

import edu.umn.cs5115.scheduler.entities.CoursePart;
import edu.umn.cs5115.scheduler.entities.Schedule;
import edu.umn.cs5115.scheduler.framework.Document;
import edu.umn.cs5115.scheduler.framework.KeyValueAdapter;
import edu.umn.cs5115.scheduler.framework.KeyValueCoding;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver.SetMutationType;
import edu.umn.cs5115.scheduler.framework.WindowController;
import edu.umn.cs5115.scheduler.framework.gui.SelectionController;
import edu.umn.cs5115.scheduler.search.SearchWindowController;
import java.util.Set;

/**
 * This is the main document for the scheduler project.
 * @author grant
 */
public class SchedulerDocument extends Document
{
    /** Manages the documents selection. 
     * @see #getSelectionController() */
    private SelectionController<CoursePart> currentSelection = new SelectionController<CoursePart>();
    /** The main (and only) schedule. */
    private Schedule schedule;
    /** The search window controller, so that we can show it again after using 
     * it once. (rather than just having many copies created.) */
    private SearchWindowController searchWindowController;
    /** Listens to the schedule, and deselects freshly deleted items. */
    private KeyValueObserver selectionRemovalListener = new KeyValueAdapter()
    {
        public void valueChanged(KeyValueCoding object, String key, SetMutationType mutation, Set modifyingSet)
        {
            if (!schedule.getCourses().contains(currentSelection.getSelection()))
                currentSelection.setSelection(null);
        }
    };
    
    /** Creates a new instance of SchedulerDocument */
    public SchedulerDocument()
    {
        schedule = new Schedule(this);
        schedule.addListener(selectionRemovalListener, Schedule.COURSES_KEY);
    }

    /**
     * Create (and show) the window controllers for the document.
     */
    protected void createWindowControllers()
    {
        MainWindowController mainWindow = new MainWindowController(this);
        mainWindow.showWindow();
    }
    
    /**
     * Get the object that is manages the current selection in the document.
     * This represents the selected course in the left sidebar and in the 
     * calendar view.
     * @return The object managing the document's selection
     */
    public SelectionController<CoursePart> getSelectionController()
    {
        return currentSelection;
    }
    
    /**
     * Get the main (and only) schedule.
     */
    public Schedule getSchedule()
    {
        return schedule;
    }

    /**
     * Gets the document's search window controller.  This creates a window 
     * controller for the window and returns it if it is not already open.
     * It does not actually show the window.
     * @return The document's search window controller, creating a new one if
     * there's not already one.
     */
    public SearchWindowController getSearchWindowController()
    {
        if (searchWindowController == null)
            searchWindowController = new SearchWindowController(this);
        return searchWindowController;
    }
    
    /**
     * Removes a window controller from the document.  Overriden so that we
     * can know when our SearchWindowController is removed, and will need to be
     * created anew.
     * @param windowController The window controller to remove from this 
     * document.
     */
    public void removeWindowController(WindowController windowController)
    {
        super.removeWindowController(windowController); 
        if (windowController == searchWindowController)
            searchWindowController = null;
    }
}
